package com.bytedance.common.plugin.interfaces.pushmanager.setting;

import android.annotation.SuppressLint;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.UriMatcher;
import android.content.pm.ProviderInfo;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Build;
import android.support.design.a;
import android.text.TextUtils;

import com.bytedance.common.utility.Logger;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PushMultiProcessSharedProvider extends ContentProvider {
    public static class Editor {
        Context mContext;
        private ContentValues mValues;

        private Editor(Context arg2) {

            this.mValues = new ContentValues();
            this.mContext = arg2.getApplicationContext();
        }

        public void apply() {
            synchronized (this) {
                this.mContext.getContentResolver().insert(PushMultiProcessSharedProvider.getContentUri(this.mContext, "key", "type"), this.mValues);
            }
        }

        public void clear() {
            try {
                this.mContext.getContentResolver().delete(PushMultiProcessSharedProvider.getContentUri(this.mContext, "key", "type"), null, null);
            } catch (Throwable v0) {
            }
        }

        public void commit() {
            synchronized (this) {
                this.apply();
            }
        }

        public Editor putBoolean(String arg3, boolean arg4) {
            this.mValues.put(arg3, Boolean.valueOf(arg4));
            return this;
        }

        public Editor putFloat(String arg3, float arg4) {
            this.mValues.put(arg3, Float.valueOf(arg4));
            return this;
        }

        public Editor putInt(String arg3, int arg4) {
            this.mValues.put(arg3, Integer.valueOf(arg4));
            return this;
        }

        public Editor putLong(String arg3, long arg4) {
            this.mValues.put(arg3, Long.valueOf(arg4));
            return this;
        }

        public Editor putString(String arg2, String arg3) {
            this.mValues.put(arg2, arg3);
            return this;
        }

        public void remove(String arg2) {
            this.mValues.putNull(arg2);
        }
    }

    public static class MultiProcessShared {
        private Context mContext;

        private MultiProcessShared(Context arg3) {

            this.mContext = arg3.getApplicationContext();
            if (Logger.debug()) {
                Logger.d("PushService", "MultiProcessShared create");
            }
        }

        public Editor edit() {
            return new Editor(this.mContext);
        }

        public boolean getBoolean(String arg7, boolean arg8) {
            try {
                arg8 = PushMultiProcessSharedProvider.getBooleanValue(this.mContext.getContentResolver().query(PushMultiProcessSharedProvider.getContentUri(this.mContext, arg7, "boolean"), null, null, null, null), arg8);
            } catch (Throwable v0) {
            }

            return arg8;
        }

        public float getFloat(String arg7, float arg8) {
            try {
                arg8 = PushMultiProcessSharedProvider.getFloatValue(this.mContext.getContentResolver().query(PushMultiProcessSharedProvider.getContentUri(this.mContext, arg7, "float"), null, null, null, null), arg8);
            } catch (Throwable v0) {
            }

            return arg8;
        }

        public int getInt(String arg7, int arg8) {
            try {
                arg8 = PushMultiProcessSharedProvider.getIntValue(this.mContext.getContentResolver().query(PushMultiProcessSharedProvider.getContentUri(this.mContext, arg7, "integer"), null, null, null, null), arg8);
            } catch (Throwable v0) {
            }

            return arg8;
        }

        public long getLong(String arg7, long arg8) {
            try {
                arg8 = PushMultiProcessSharedProvider.getLongValue(this.mContext.getContentResolver().query(PushMultiProcessSharedProvider.getContentUri(this.mContext, arg7, "long"), null, null, null, null), arg8);
            } catch (Throwable v0) {
            }

            return arg8;
        }

        public String getString(String arg7, String arg8) {
            try {
                arg8 = PushMultiProcessSharedProvider.getStringValue(this.mContext.getContentResolver().query(PushMultiProcessSharedProvider.getContentUri(this.mContext, arg7, "string"), null, null, null, null), arg8);
            } catch (Throwable v0) {
            }

            return arg8;
        }
    }

    public static final String ALL_TYPE = "all";
    public static final String BOOLEAN_TYPE = "boolean";
    public static final String FLOAT_TYPE = "float";
    public static final String INT_TYPE = "integer";
    private static final String KEY = "key";
    private static final String KEY_COLUMN = "key_column";
    public static final String LONG_TYPE = "long";
    private static final int MATCH_DATA = 65536;
    private static final String SP_CONFIG_NAME = "push_multi_process_config";
    public static final String STRING_TYPE = "string";
    private static final String TYPE = "type";
    private static final String TYPE_COLUMN = "type_column";
    private static final String VALUE_COLUMN = "value_column";
    private Map mContentValues;
    private SharedPreferences mSharedPreferences;
    public static Uri sBaseUri;
    private static MultiProcessShared sInstance;
    private static UriMatcher sMatcher;
    public static String sShareAuthority;

    public PushMultiProcessSharedProvider() {

        this.mContentValues = new ConcurrentHashMap();
    }

    public int delete(Uri arg4, String arg5, String[] arg6) {
        switch (PushMultiProcessSharedProvider.sMatcher.match(arg4)) {
            case 65536: {
                try {
                    this.getMultiProcessSharedPreferences().edit().clear().commit();
                    this.mContentValues.clear();
                    this.notifyContentChanged(PushMultiProcessSharedProvider.getContentUri(this.getContext(), "key", "type"));
                } catch (Exception v0) {
                }
                return 0;
            }
        }

        throw new IllegalArgumentException("Unsupported uri " + arg4);


    }

    public static Editor edit(Context arg2) {
        return new Editor(arg2);
    }

    private static boolean getBooleanValue(Cursor arg2, boolean arg3) {
        boolean v0 = false;
        if (arg2 != null) {
            try {
                if (arg2.moveToFirst()) {
                    if (arg2.getInt(0) <= 0) {

                    } else {
                        v0 = true;
                    }
                    arg3 = v0;
                    return arg3;
                }
            } catch (Throwable v0_1) {

            } finally {
                if (arg2 != null) {
                    try {
                        arg2.close();
                    } catch (Exception v1) {
                    }
                }
            }
        }

        return arg3;
    }

    public static final Uri getContentUri(Context arg3, String arg4, String arg5) {
        Uri v0_2 = null;
        Class v1 = PushMultiProcessSharedProvider.class;
        synchronized (v1) {
            if (PushMultiProcessSharedProvider.sBaseUri == null) {
                try {
                    if (Logger.debug()) {
                        Logger.d("MultiProcessSharedProvider", "init form getContentUri");
                    }

                    PushMultiProcessSharedProvider.init(arg3);
                } catch (Exception v0_1) {
                    v0_1.printStackTrace();
                    v0_2 = null;
                    return v0_2;
                }
            }
            v0_2 = PushMultiProcessSharedProvider.sBaseUri.buildUpon().appendPath(arg4).appendPath(arg5).build();
            return v0_2;
        }
    }

    private static float getFloatValue(Cursor arg2, float arg3) {
        if (arg2 != null) {
            try {
                if (arg2.moveToFirst()) {
                    arg3 = arg2.getFloat(0);
                }
            } catch (Throwable v0) {

            } finally {
                if (arg2 != null) {
                    try {
                        arg2.close();
                    } catch (Exception v1) {
                    }
                }
            }
        }

        return arg3;
    }

    private static int getIntValue(Cursor arg2, int arg3) {
        if (arg2 != null) {
            try {
                if (arg2.moveToFirst()) {
                    arg3 = arg2.getInt(0);
                }
            } catch (Throwable v0) {

            } finally {
                if (arg2 != null) {
                    try {
                        arg2.close();
                    } catch (Exception v0_1) {
                    }
                } else {
                }

                return arg3;
            }
        }

        return arg3;
    }

    private static long getLongValue(Cursor arg3, long arg4) {
        if (arg3 != null) {
            try {
                if (arg3.moveToFirst()) {
                    arg4 = arg3.getLong(0);
                }
            } catch (Throwable v0) {
            } finally {
                if (arg3 != null) {
                    try {
                        arg3.close();
                    } catch (Exception v1) {
                    }

                }
            }
        }

        return arg4;
    }

    private SharedPreferences getMultiProcessSharedPreferences() {
        SharedPreferences v0_2;
        String v2;
        Context v1;
        int v0_1;
        synchronized (this) {
            if (this.mSharedPreferences == null) {
                v0_1 = Build.VERSION.SDK_INT;
                v1 = this.getContext().getApplicationContext();
                v2 = "push_multi_process_config";
                if (v0_1 >= 11) {
                    v0_1 = 4;
                } else {
                    v0_1 = 0;
                }
                this.mSharedPreferences = v1.getSharedPreferences(v2, v0_1);
                v0_2 = this.mSharedPreferences;
                return v0_2;
            } else {
                v0_2 = this.mSharedPreferences;
                return v0_2;
            }
        }
    }

    public static MultiProcessShared getMultiprocessShared(Context arg3) {
        MultiProcessShared v0_1;
        Class v1 = PushMultiProcessSharedProvider.class;
        synchronized (v1) {
            if (PushMultiProcessSharedProvider.sInstance == null) {
                PushMultiProcessSharedProvider.sInstance = new MultiProcessShared(arg3);
            }

            v0_1 = PushMultiProcessSharedProvider.sInstance;
        }
        return v0_1;
    }

    public static String getProviderAuthority(Context arg6, String arg7) {
        int v1_1;
        String v0 = null;
        if (arg6 != null && !a.isTextEmpty(arg7)) {
            try {
                ProviderInfo[] v2 = arg6.getPackageManager().getPackageInfo(arg6.getPackageName(), 8).providers;
                int v3 = v2.length;
                v1_1 = 0;
                while (true) {
                    if (v1_1 >= v3) {
                        return v0;
                    }
                    ProviderInfo v4 = v2[v1_1];
                    if (!arg7.equals(v4.name)) {
                        ++v1_1;
                        continue;
                    }
                    return v4.authority;
                }
            } catch (Exception v1) {
                return v0;
            }
        }

        return v0;
    }

    private static String getStringValue(Cursor arg2, String arg3) {
        if (arg2 != null) {
            try {
                if (arg2.moveToFirst()) {
                    arg3 = arg2.getString(0);
                }
            } catch (Throwable v0) {

            } finally {
                if (arg2 != null) {
                    try {
                        arg2.close();
                    } catch (Exception v1) {
                    }
                }
            }
        }

        return arg3;
    }

    public String getType(Uri arg3) {
        return "vnd.android.cursor.item/vnd." + PushMultiProcessSharedProvider.sShareAuthority + ".item";
    }

    private static void init(Context arg4) throws IllegalStateException {
        String v0 = PushMultiProcessSharedProvider.getProviderAuthority(arg4, PushMultiProcessSharedProvider.class.getName());
        PushMultiProcessSharedProvider.sShareAuthority = v0;
        if (TextUtils.isEmpty(((CharSequence) v0))) {
            throw new IllegalStateException("Must Set MultiProcessSharedProvider Authority");
        }

        if (Logger.debug()) {
            Logger.d("MultiProcessSharedProvider", PushMultiProcessSharedProvider.sShareAuthority);
        }

        UriMatcher v0_1 = new UriMatcher(-1);
        PushMultiProcessSharedProvider.sMatcher = v0_1;
        v0_1.addURI(PushMultiProcessSharedProvider.sShareAuthority, "*/*", 65536);
        PushMultiProcessSharedProvider.sBaseUri = Uri.parse("content://" + PushMultiProcessSharedProvider.sShareAuthority);
    }

    @SuppressLint(value = {"NewApi"})
    public Uri insert(Uri arg11, ContentValues arg12) {
        SharedPreferences.Editor v2_2;
        Object v1;
        Object v0_1;
        SharedPreferences.Editor v4;
        Uri v5 = null;
        switch (PushMultiProcessSharedProvider.sMatcher.match(arg11)) {
            case 65536: {
                break;
            }
            default:
                throw new IllegalArgumentException("Unsupported uri " + arg11);

        }

        try {
            Iterator v6 = arg12.valueSet().iterator();
            v4 = ((SharedPreferences.Editor) v5);
            while (true) {
                label_16:
                if (!v6.hasNext()) {
                    if (v4 != null) {
                        if (Build.VERSION.SDK_INT > 8) {
                            v4.apply();
                        } else {
                            v4.commit();
                        }
                    }
                    return v5;
                }

                v0_1 = v6.next();
                v1 = ((Map.Entry) v0_1).getValue();
                v0_1 = ((Map.Entry) v0_1).getKey();
                if (Logger.debug()) {
                    Logger.d("PushService", "MultiProcessShareProvider insert key = " + (((String) v0_1)) + " value = " + v1.toString());
                }

                int v2 = 0;
                if (v1 == null) {
                    this.mContentValues.remove(v0_1);
                    v2 = 1;
                } else {
                    Object v7 = this.mContentValues.get(v0_1);
                    if (v7 != null && (v7.equals(v1))) {
                    } else {
                        this.mContentValues.put(v0_1, v1);
                        v2 = 1;
                    }
                }

                if (v2 == 0) {
                    continue;
                }

                if (Logger.debug()) {
                    String v7_1 = "PushService";
                    String v2_1 = "MultiProcessShareProvider reallly insert key = " + (((String) v0_1)) + " value = " + v1 != null ? v1.toString() : "null";
                    Logger.d(v7_1, v2_1);
                }

                if (v4 == null) {
                    v2_2 = this.getMultiProcessSharedPreferences().edit();
                } else {
                    v2_2 = v4;
                }
                try {
                    if (v1 == null) {
                        v2_2.remove(((String) v0_1));
                        v4 = v2_2;
                        continue;
                    }

                    if ((v1 instanceof String)) {
                        v2_2.putString(((String) v0_1), ((String) v1));
                        this.notifyContentChanged(PushMultiProcessSharedProvider.getContentUri(this.getContext(), ((String) v0_1), "string"));
                        v4 = v2_2;
                        continue;
                    }

                    if ((v1 instanceof Boolean)) {
                        v2_2.putBoolean(((String) v0_1), ((Boolean) v1).booleanValue());
                        this.notifyContentChanged(PushMultiProcessSharedProvider.getContentUri(this.getContext(), ((String) v0_1), "boolean"));
                        v4 = v2_2;
                        continue;
                    }

                    if ((v1 instanceof Long)) {
                        v2_2.putLong(((String) v0_1), ((Long) v1).longValue());
                        this.notifyContentChanged(PushMultiProcessSharedProvider.getContentUri(this.getContext(), ((String) v0_1), "long"));
                        v4 = v2_2;
                        continue;
                    }

                    if ((v1 instanceof Integer)) {
                        v2_2.putInt(((String) v0_1), ((Integer) v1).intValue());
                        this.notifyContentChanged(PushMultiProcessSharedProvider.getContentUri(this.getContext(), ((String) v0_1), "integer"));
                        v4 = v2_2;
                        continue;
                    }

                    if ((v1 instanceof Float)) {
                        v2_2.putFloat(((String) v0_1), ((Float) v1).floatValue());
                        this.notifyContentChanged(PushMultiProcessSharedProvider.getContentUri(this.getContext(), ((String) v0_1), "float"));
                        v4 = v2_2;
                        continue;
                    }

                    throw new IllegalArgumentException("Unsupported type " + arg11);
                } catch (Exception v0) {

                }
                return v5;
            }
        } catch (Exception v0) {
            return v5;
        }
    }

    private void loadValues() {
        SharedPreferences v0 = this.getMultiProcessSharedPreferences();
        if (v0 != null) {
            Iterator v2 = v0.getAll().entrySet().iterator();
            while (v2.hasNext()) {
                Object v0_1 = v2.next();
                this.mContentValues.put(((Map.Entry) v0_1).getKey(), ((Map.Entry) v0_1).getValue());
            }
        }
    }

    private void notifyContentChanged(Uri arg3) {
        this.getContext().getContentResolver().notifyChange(arg3, null);
    }

    public boolean onCreate() {
        if (PushMultiProcessSharedProvider.sMatcher == null) {
            try {
                if (Logger.debug()) {
                    Logger.d("MultiProcessSharedProvider", "init form onCreate");
                }

                PushMultiProcessSharedProvider.init(this.getContext());
                this.loadValues();
            } catch (Exception v0) {
                v0.printStackTrace();
                boolean v0_1 = false;
                return v0_1;
            }
        }

        try {
            Intent v0_3 = new Intent();
            v0_3.setAction("com.ss.android.message");
            v0_3.setPackage(this.getContext().getPackageName());
            this.getContext().startService(v0_3);
        } catch (Throwable v0_2) {
            v0_2.printStackTrace();
        }

        return true;
    }

    public Cursor query(Uri arg11, String[] arg12, String arg13, String[] arg14, String arg15) {
        String v9;
        String v0_5;
        Object v5_2;
        String v5_1;
        MatrixCursor.RowBuilder v7;
        MatrixCursor v0_4;
        int v1_2;
        MatrixCursor.RowBuilder v5;
        Object v1_1;
        Cursor v0_3;
        Object v0_2;
        Iterator v6;
        MatrixCursor v2_1;
        Map v0_1;
        Cursor v1 = null;
        switch (PushMultiProcessSharedProvider.sMatcher.match(arg11)) {
            case 65536: {
                try {
                    String v2 = "all";
                    if (!v2.equals(arg11.getPathSegments().get(1))) {
                        try {
                            label_93:
                            v0_2 = arg11.getPathSegments().get(0);
                            boolean v2_2 = this.mContentValues.containsKey(v0_2);
                            if (!v2_2) {
                                return null;
                            }else {
                                v2_1 = new MatrixCursor(new String[]{(String) v0_2});
                                v1_1 = this.mContentValues.get(v0_2);
                                v5 = v2_1.newRow();
                                if ((v1_1 instanceof Boolean)) {
                                    if (((Boolean) v1_1).booleanValue()) {
                                        v1_2 = 1;
                                    } else {
                                        v1_2 = 0;
                                    }
                                    v1_1 = Integer.valueOf(v1_2);
                                }
                                if (Logger.debug()) {
                                    Logger.d("PushService", "MultiProcessShareProvider reallly get key = " + v0_2 + " value = " + v1_1.toString());
                                }
                                v5.add(v1_1);
                                v0_4 = v2_1;
                            }
                            return ((Cursor) v0_4);
                        } catch (Exception v0) {
                            return ((Cursor) null);
                        }
                    }
                    v0_1 = this.getMultiProcessSharedPreferences().getAll();
                    v2_1 = new MatrixCursor(new String[]{"key_column", "value_column", "type_column"});
                    v6 = v0_1.entrySet().iterator();
                    try {
                        while (true) {
                            if (!v6.hasNext()) {
                                v0_4 = v2_1;
                                return v0_4;
                            }
                            v0_2 = v6.next();
                            v1_1 = ((Map.Entry) v0_2).getKey();
                            v0_2 = ((Map.Entry) v0_2).getValue();
                            v7 = v2_1.newRow();
                            v5_1 = "string";
                            if ((v0_2 instanceof String)) {
                                v5_2 = v0_2;
                                v0_5 = "string";
                            } else if ((v0_2 instanceof Boolean)) {
                                v5_1 = "boolean";
                                int v0_6 = ((Boolean) v0_2).booleanValue() ? 1 : 0;
                                v9 = v5_1;
                                v5_2 = Integer.valueOf(v0_6);
                                v0_5 = v9;
                            } else {
                                if ((v0_2 instanceof Integer)) {
                                    v5_2 = v0_2;
                                    v0_5 = "integer";
                                }else

                                if ((v0_2 instanceof Long)) {
                                    v5_2 = v0_2;
                                    v0_5 = "long";
                                }else

                                if (!(v0_2 instanceof Float)) {
                                    v9 = v5_1;
                                    v5_2 = v0_2;
                                    v0_5 = v9;
                                }else {
                                    v5_2 = v0_2;
                                    v0_5 = "float";
                                }
                            }
                            v7.add(v1_1);
                            v7.add(v5_2);
                            v7.add(v0_5);
                            continue;
                        }
                    } catch (Exception v0) {

                    }
                } catch (Exception v0) {
                    return null;
                }
            }
        }

        throw new IllegalArgumentException("Unsupported uri " + arg11);

    }

    public int update(Uri arg2, ContentValues arg3, String arg4, String[] arg5) {
        throw new UnsupportedOperationException();
    }
}

